package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysStuBase;
import com.excellent.system.domain.SysStuTutor;
import com.excellent.system.domain.SysTeaBase;
import com.excellent.system.domain.SysTeaStu;
import com.excellent.system.service.ISysDictDataService;
import com.excellent.system.service.ISysStuTutorService;
import com.excellent.system.service.ISysTeaStuService;
import com.excellent.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 对接导师Controller
 *
 * @author YYY
 * @date 2021-04-20
 */
@Controller
@RequestMapping("/system/tutor")
public class SysStuTutorController extends BaseController
{
    private String prefix = "system/tutor";


    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysStuTutorService sysStuTutorService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysTeaStuService sysTeaStuService;

    @GetMapping()
    public String tutor()
    {
        return prefix + "/tutor";
    }

    /**
     * 教师端确认选择 check
     */
    @GetMapping("/check")
    public String check(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        List<SysTeaBase> list =sysUserService.selectSysTeaBaseList(user);
        SysTeaBase sysTeaBase=list.get(0);
        String title = sysTeaBase.getTeaTitle();
        int count =sysTeaStuService.selectCountSysTeaStuById(sysTeaBase.getTeaJno());
        String StitleCount = sysDictDataService.selectDictLabel("sys_tea_title", title);
        int titleCount=StitleCount.charAt(0)-'0';
        mmap.put("titleCount",titleCount);
        mmap.put("count",count);
        return prefix + "/check";
    }


    /**
     * 查询已选择学生列表
     */
    @PostMapping("/stuList")
    @ResponseBody
    public TableDataInfo stuList(SysStuTutor sysStuTutor)
    {
        SysUser user=ShiroUtils.getSysUser();
        SysStuTutor sysStuTutor1=new SysStuTutor();
        sysStuTutor1.setTea1Jno(user.getLoginName());
        SysStuTutor sysStuTutor2=new SysStuTutor();
        sysStuTutor2.setTea2Jno(user.getLoginName());
        List<SysStuTutor> list = sysStuTutorService.selectSysStuTutorList(sysStuTutor1);
        List<SysStuTutor> list2 = sysStuTutorService.selectSysStuTutorList(sysStuTutor2);
        list.addAll(list2);
        List<SysStuTutor> list1 =new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            SysStuTutor sysStuTutor3 =list.get(i);
            SysStuTutor sysStuTutor4= sysStuTutorService.selectSysStuTutorById(sysStuTutor3.getStuSno());
            if(null==sysStuTutor4.getTeaJno()||sysStuTutor4.getTeaJno().length()==0)
            {
                list1.add(sysStuTutor4);
            }
        }
        return getDataTable(list1);
    }



    /**
     * 查询已双向选择学生列表
     */
    @PostMapping("/myStuList")
    @ResponseBody
    public TableDataInfo myStuList(SysStuTutor sysStuTutor)
    {
        SysUser user=ShiroUtils.getSysUser();
        SysStuTutor sysStuTutor1=new SysStuTutor();
        sysStuTutor1.setTeaJno(user.getLoginName());
        List<SysStuTutor> list = sysStuTutorService.selectSysStuTutorList(sysStuTutor1);
        List<SysStuTutor> list1 =new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            SysStuTutor sysStuTutor2 =list.get(i);
            SysStuTutor sysStuTutor4= sysStuTutorService.selectSysStuTutorById(sysStuTutor2.getStuSno());
            list1.add(sysStuTutor4);
        }
        return getDataTable(list1);
    }



    /**
     * 查询对接导师列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStuTutor sysStuTutor)
    {
        startPage();
        List<SysStuTutor> list = sysStuTutorService.selectSysStuTutorList(sysStuTutor);
        return getDataTable(list);
    }

    /**
     * 查询对接导师列表
     */
    @PostMapping("/noticeList")
    @ResponseBody
    public AjaxResult noticeList(SysStuTutor sysStuTutor)
    {
        List<SysStuTutor> list = sysStuTutorService.selectSysStuTutorList(sysStuTutor);
        StringBuffer result= new StringBuffer();
        for(int i=0;i<list.size();i++){
            result.append("<p>"+list.get(i).getStuSno()+";</p>");
        }
        System.out.println(result);
        return AjaxResult.success(result);
    }
    /**
     * 导出对接导师列表
     */
    @Log(title = "对接导师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStuTutor sysStuTutor)
    {
        List<SysStuTutor> list = sysStuTutorService.selectSysStuTutorList(sysStuTutor);
        ExcelUtil<SysStuTutor> util = new ExcelUtil<SysStuTutor>(SysStuTutor.class);
        return util.exportExcel(list, "tutor");
    }

    /**
     * 新增对接导师
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存对接导师
     */
    @Log(title = "对接导师", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStuTutor sysStuTutor)
    {

        return toAjax(sysStuTutorService.insertSysStuTutor(sysStuTutor));
    }

    /**
     * 修改对接导师
     */
    @GetMapping("/edit/{stuSno}")
    public String edit(@PathVariable("stuSno") String stuSno, ModelMap mmap)
    {
        SysStuTutor sysStuTutor = sysStuTutorService.selectSysStuTutorById(stuSno);
        mmap.put("sysStuTutor", sysStuTutor);
        return prefix + "/edit";
    }

    /**
     * 修改保存对接导师
     */
    @Log(title = "对接导师", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStuTutor sysStuTutor)
    {
        return toAjax(sysStuTutorService.updateSysStuTutor(sysStuTutor));
    }


    //导师端确认选择
    @Log(title = "对接导师", businessType = BusinessType.UPDATE)
    @PostMapping("/editTea")
    @ResponseBody
    public AjaxResult editTea(String stuSno)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        String[] s=stuSno.split(",");
        int coun=0;
        List<SysTeaBase> list =sysUserService.selectSysTeaBaseList(user);
        SysTeaBase sysTeaBase=list.get(0);
        String title = sysTeaBase.getTeaTitle();
        String StitleCount = sysDictDataService.selectDictLabel("sys_tea_title", title);
        int titleCount=StitleCount.charAt(0)-'0';

        for (int i=0;i<s.length;i++)
        {
            SysStuTutor sysStuTutor = sysStuTutorService.selectSysStuTutorById(s[i]);
            sysStuTutor.setTeaJno(user.getLoginName());
            sysStuTutorService.updateSysStuTutor(sysStuTutor);

            SysTeaStu sysTeaStu=new SysTeaStu();
            sysTeaStu.setStuSno(s[i]);
            sysTeaStu.setTeaJno(user.getLoginName());
            sysTeaStuService.insertSysTeaStu(sysTeaStu);
        }
        coun= sysTeaStuService.selectCountSysTeaStuById(sysTeaBase.getTeaJno());
        //如果当前教师已经选择满了，清除选择他的学生对应的选择项。
        if(coun==titleCount){
            SysStuTutor sysStuTutor = new SysStuTutor();
            sysStuTutor.setTea1Jno(sysTeaBase.getTeaJno());
            List<SysStuTutor> sysStuTutors = sysStuTutorService.selectSysStuTutorList(sysStuTutor);
            SysStuTutor sysStuTutor1 = new SysStuTutor();
            sysStuTutor1.setTea2Jno(sysTeaBase.getTeaJno());
            List<SysStuTutor> sysStuTutors1 = sysStuTutorService.selectSysStuTutorList(sysStuTutor1);
            if(sysStuTutors!=null){
                for(int i=0;i<sysStuTutors.size();i++)
                {
                    sysStuTutor =sysStuTutors.get(i);
                    sysStuTutor.setTea1Jno("");
                    sysStuTutor.setIntention1("");
                    sysStuTutorService.updateSysStuTutor(sysStuTutor);
                }
            }
            if(sysStuTutors1!=null){
                for(int i=0;i<sysStuTutors1.size();i++)
                {
                    sysStuTutor1 =sysStuTutors1.get(i);
                    sysStuTutor1.setTea2Jno("");
                    sysStuTutor1.setIntention2("");
                    sysStuTutorService.updateSysStuTutor(sysStuTutor1);
                }
            }

        }
        return toAjax(coun);
    }

    @Log(title = "对接导师", businessType = BusinessType.UPDATE)
    @PostMapping("/editTeaTwo")
    @ResponseBody
    public AjaxResult editTeaTwo(String intention1,String tea1Jno,String intention2,String tea2Jno )
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin()){
            return toAjax(1);
        }
        else {
            SysStuTutor sysStuTutor =sysStuTutorService.selectSysStuTutorById(user.getLoginName());

            if( null == sysStuTutor.getTea1Jno() || sysStuTutor.getTea1Jno().length()==0)
            {
                sysStuTutor.setTea1Jno(tea1Jno);
                sysStuTutor.setIntention1(intention1);
            }
            if(null == sysStuTutor.getTea2Jno() || sysStuTutor.getTea2Jno().length()==0 )
            {
                sysStuTutor.setTea2Jno(tea2Jno);
                sysStuTutor.setIntention2(intention2);
            }
            return toAjax(sysStuTutorService.updateSysStuTutor(sysStuTutor));
        }

    }

    @Log(title = "对接导师", businessType = BusinessType.UPDATE)
    @PostMapping("/editTeaOne")
    @ResponseBody
    public AjaxResult editTeaOne(String intention1,String tea1Jno)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        SysStuTutor sysStuTutor =sysStuTutorService.selectSysStuTutorById(user.getLoginName());

        if( null == sysStuTutor.getTea1Jno() || sysStuTutor.getTea1Jno().length()==0)
        {
            sysStuTutor.setTea1Jno(tea1Jno);
            sysStuTutor.setIntention1(intention1);
        }
        else if(null == sysStuTutor.getTea2Jno() || sysStuTutor.getTea2Jno().length()==0 )
        {
            sysStuTutor.setTea2Jno(tea1Jno);
            sysStuTutor.setIntention2(intention1);
        }
        return toAjax(sysStuTutorService.updateSysStuTutor(sysStuTutor));

    }
    /**
     * 删除对接导师
     */
    @Log(title = "对接导师", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStuTutorService.deleteSysStuTutorByIds(ids));
    }

    /**
     * 选择导师
     */
    @GetMapping("/chooseOne")
    public String chooseOne()
    {
        return prefix + "/chooseOne";
    }
    /**
     * 选择导师
     */
    @GetMapping("/chooseTwo")
    public String chooseTwo()
    {
        return prefix + "/chooseTwo";
    }

//    /**
//     * 确认学生
//     */
//    @GetMapping("/confirm")
//    public String confirm()
//    {
//        return prefix + "/confirm";
//    }
    /**
     *
     * 教师选择确认
     */
    @GetMapping("/teaConfirm")
    public String teaConfirm(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        List<SysTeaBase> list =sysUserService.selectSysTeaBaseList(user);
        SysTeaBase sysTeaBase=list.get(0);
        String title = sysTeaBase.getTeaTitle();
        int count =sysTeaStuService.selectCountSysTeaStuById(sysTeaBase.getTeaJno());
        String StitleCount = sysDictDataService.selectDictLabel("sys_tea_title", title);
        int titleCount=StitleCount.charAt(0)-'0';
        mmap.put("titleCount",titleCount);
        mmap.put("count",count);
        return prefix + "/teaConfirm";
    }
    /**
     * 学生选择确认
     */
    @GetMapping("/stuConfirm")
    public String stuConfirm()
    {
        return prefix + "/stuConfirm";
    }


}

